package com.bxs.dao.db;

import com.bxs.dao.BaseDataHandler;
import com.bxs.mapper.RecordMapper;
import com.bxs.pojo.DangerMsg;
import com.bxs.pojo.Record;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @author dream
 * @title: SaveMsgToDBHandler
 * @projectName ids_v0_2
 * @description: TODO
 */
// 存储检测结果到数据库的处理器实现
@Service
@Slf4j
public class SaveMsgToDBHandler extends BaseDataHandler {
    @Autowired
    RecordMapper recordMapper;

    // 多线程写入需要考虑线程安全问题
    @Override
    public synchronized void handleData(DangerMsg data) {
        System.out.println("SaveMsgToDBHandler handle data start");
        // 处理结果存储到数据库
        Record res = new Record();
        res.setDescription(data.getDesc());
        res.setLogLevel(data.getCode());
        res.setSourceIp(data.getSourceIp());
        res.setSourcePort(data.getSourcePort());
        res.setTargetPort(data.getSourcePort());
        res.setTargetIp(data.getTargetIp());
        System.out.println("insert data: " + res);
        try {
            int insert = recordMapper.insert(res);
            System.out.println("SaveMsgToDBHandler handle data count: " + insert);
        }catch (Exception e){
            System.out.println("insert error: " + e);
            e.printStackTrace();
        }
        log.info("SaveMsgToDBHandler execute...");
    }
}
